package com.tarena.mvc.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.tarena.mvc.domain.Product;
import com.tarena.mvc.service.ProductDao;
import com.tarena.mvc.util.WphUtils;

/**
 * 
 * @author WangPeihu
 * @since 29/12/2009
 */
public class ActionServlet extends HttpServlet {

	private static final long serialVersionUID = -5069264022753187041L;
	ProductDao pd = new ProductDao();
	private static final String prefix = "/product/";

	/**
	 * 用于处理客户端发来的各种请求，并返回
	 * <p>
	 * 1. product_list :显示所有商品列表页面地址
	 * </p>
	 * <p>
	 * 2. product_form :添加商品表单页面地址
	 * </p>
	 * <p>
	 * 3. product_add :添加商品业务地址
	 * </p>
	 * <p>
	 * 4. product_delete :删除一个商品
	 * </p>
	 * <p>
	 * 5. product_update :更新一个商品
	 * </p>
	 * <p>
	 * 6. product_detail :商品的详细表单
	 * </p>
	 */
	@Override
	public void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String url = request.getRequestURI();
		String path = url.substring(url.indexOf(prefix) + prefix.length());
		System.out.println(path);

		/** 1 、检测是否是查看列表请求 */
		if ("product_list".equals(path)) {
			List<Product> productList = pd.getProduct();
			for (Product p : productList) {
				System.out.println(p.getBrand());
			}
			request.setAttribute("productList", productList);
			RequestDispatcher rd = request
					.getRequestDispatcher("/WEB-INF/jsp/product_list.jsp");
			rd.forward(request, response);
		}

		/** 2、 检测是否为增加表单请求 */
		if ("product_form".equals(path)) {
			RequestDispatcher rd = request
					.getRequestDispatcher("/WEB-INF/jsp/product_form.jsp");
			rd.forward(request, response);
		}

		/** 3、检测是否添加产品请求 */
		if ("product_add".equals(path)) {
			Product product = new Product();
			request.setCharacterEncoding("utf-8");
			WphUtils.populate(product, request);
			pd.InsertProduct(product);
			response.sendRedirect("product_list");
		}

		/** 4、检测是否是删除请求 */
		if ("product_delete".equals(path)) {
			String[] pId = request.getParameterValues("productId");
			if (pId != null && pId.length > 0) {
				int[] productIds = new int[pId.length];
				for (int i = 0; i < pId.length; i++) {
					productIds[i] = Integer.parseInt(pId[i]);
				}
				pd.deleteProductsById(productIds);
			}
			response.sendRedirect("product_list");
		}

		/** 5、检测是否为更新请求 */
		if ("product_update".equals(path)) {
			request.setCharacterEncoding("utf-8");
			Product product = new Product();
			WphUtils.populate(product, request);
			System.out.println(product.toString());
			pd.updateProduct(product);
			response.sendRedirect("product_list");
		}

		/** 6、检测是否为商品的详细表单请求 */
		if ("product_detail".equals(path)) {
			request.setCharacterEncoding("utf-8");
			String pid = request.getParameter("id");
			Integer id = -1;
			id = Integer.parseInt(pid);
			Product product = pd.getProductById(id);
			request.setAttribute("product", product);
			RequestDispatcher rd = request
					.getRequestDispatcher("/WEB-INF/jsp/product_detail.jsp");
			rd.forward(request, response);
		}
	}

}
